﻿using System;
using System.Web.UI.WebControls;
using Business;
using System.Globalization;
namespace ServicesDirectory.UserControl
{
    public partial class LookupBusinessType : System.Web.UI.UserControl
    {
        /// <summary>
        /// Occurs when [close control].
        /// </summary>
        public event EventHandler CloseControl;
        /// <summary>
        /// Occurs when [get business].
        /// </summary>
        public event EventHandler GetBusiness;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            btnBackAll.Click += (BackAll_Click);
            btnBacklist.Click += (Backlist_Click);
            btnClose.Click += (Close_Click);
            cmdClose.Click += (Close_Click);
            btnNext.Click += (Next_Click);
            btnNextAll.Click += (NextAll_Click);
            btnNone.Click += (None_Click);
            btnSearch.Click += (Search_Click);
            btnSelect.Click += (Select_Click);
            btnClear.Click += (Clear_Click);
            rptBusiness.ItemDataBound += (Business_ItemDataBound);
            if(!IsPostBack)
                Search();

        }

        void Clear_Click(object sender, EventArgs e)
        {
            txtBusinessName.Text = "";
            txtSicCode.Text = "";
            
            lblMessage.Text = "";
        }

        /// <summary>
        /// Handles the ItemDataBound event of the Business control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RepeaterItemEventArgs" /> instance containing the event data.</param>
        private void Business_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;

            var rdo = (RadioButton)e.Item.FindControl("rbtSelect");
            const string script = "SetUniqueRadioButton(this)";
            rdo.Attributes.Add("onclick", script);
        }

        /// <summary>
        /// Searches this instance.
        /// </summary>
        private void Search()
        {
            string message;
            var lstBusiness = BusinessTypeBusiness.Instance.SearchBusiness(txtBusinessName.Text, txtSicCode.Text,
                                                                           out message);

            if (lstBusiness != null && lstBusiness.Count>0)
            {
                pnNoRecord.Visible = false;
                lblTotalPage.Text = lstBusiness.Count % 10 == 0
                                        ? (lstBusiness.Count / 10).ToString(CultureInfo.InvariantCulture)
                                        : (lstBusiness.Count / 10 + 1).ToString(CultureInfo.InvariantCulture);
                rptBusiness.DataSource =
                    BusinessTypeBusiness.Instance.PagingBusiness(
                        int.Parse(tbPageNumber.Text, CultureInfo.InvariantCulture), 10, lstBusiness);
                rptBusiness.DataBind();
            }
            else
            {
                lblTotalPage.Text = "0";
                tbPageNumber.Text = "0";
                pnNoRecord.Visible = true;
                rptBusiness.DataSource = null;
                rptBusiness.DataBind();

            }

        }


        /// <summary>
        /// Handles the Click event of the Select control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void Select_Click(object sender, EventArgs e)
        {
            var selectContact = false;
            foreach (RepeaterItem item in rptBusiness.Items)
            {

                var rdo = (RadioButton)item.FindControl("rbtSelect");
                var business = item.FindControl("hdContactId") as HiddenField;
                if (rdo.Checked)
                {
                    selectContact = true;
                    if (business != null)
                    {
                        var obusiness = BusinessTypeBusiness.Instance.GetBusiness(business.Value);
                        if(obusiness!=null)
                        if (GetBusiness != null)
                            GetBusiness(obusiness, e);
                    }

                    break;
                }
            }
            if (!selectContact)
            {
                lblMessage.Text = "you have to choose a row !";
            }
            else
            {
                if (CloseControl != null)
                {
                    txtBusinessName.Text = "";
                    txtSicCode.Text = "";
                    Search();
                    CloseControl(sender, e);
                }
            }

        }

        /// <summary>
        /// Handles the Click event of the Search control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void Search_Click(object sender, EventArgs e)
        {
            tbPageNumber.Text = "1";
            Search();
        }

        /// <summary>
        /// Handles the Click event of the None control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void None_Click(object sender, EventArgs e)
        {
            if (GetBusiness != null)
                GetBusiness(null, e);
            if (CloseControl != null)
            {
                txtBusinessName.Text = "";
                txtSicCode.Text = "";
                Search();
                CloseControl(sender, e);
            }

        }

        /// <summary>
        /// Handles the Click event of the NextAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void NextAll_Click(object sender, EventArgs e)
        {
            tbPageNumber.Text = lblTotalPage.Text;
            Search();
        }

        /// <summary>
        /// Handles the Click event of the Next control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void Next_Click(object sender, EventArgs e)
        {
            if (int.Parse(tbPageNumber.Text, CultureInfo.InvariantCulture) <
                int.Parse(lblTotalPage.Text, CultureInfo.InvariantCulture))
            {
                tbPageNumber.Text = (int.Parse(tbPageNumber.Text, CultureInfo.InvariantCulture) + 1).ToString(CultureInfo.InvariantCulture);
                Search();
            }
        }

        /// <summary>
        /// Handles the Click event of the Close control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void Close_Click(object sender, EventArgs e)
        {
            if (CloseControl != null)
            {
                txtBusinessName.Text = "";
                txtSicCode.Text = "";
                Search();
                CloseControl(sender, e);
            }
        }

        /// <summary>
        /// Handles the Click event of the Backlist control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void Backlist_Click(object sender, EventArgs e)
        {
            if (int.Parse(tbPageNumber.Text, CultureInfo.InvariantCulture) > 1)
            {
                tbPageNumber.Text = (int.Parse(tbPageNumber.Text, CultureInfo.InvariantCulture) - 1).ToString(CultureInfo.InvariantCulture);

                Search();
            }
        }

        /// <summary>
        /// Handles the Click event of the BackAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void BackAll_Click(object sender, EventArgs e)
        {
            tbPageNumber.Text = "1";
            Search();
        }
    }
}